package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.enums.OaEnums;
import com.mxpio.erp.common.oa.entity.OaInvoice;
import com.mxpio.erp.common.oa.service.OaInvoiceService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.entity.Dept;
import com.mxpioframework.security.service.DeptService;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Tag(name = "OaInvoiceController", description = "请款单")
@RestController
@RequestMapping("/erp/oa/oaInvoice/")
public class OaInvoiceController {
    @Autowired
    private OaInvoiceService oaInvoiceService;
    @Autowired
    private DeptService deptService;

    @GetMapping("list")
    @Operation(summary = "请款单列表", description = "请款单列表（无分页）", method = "GET")
    public Result<List<OaInvoice>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<OaInvoice> list = oaInvoiceService.list(OaInvoice.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "请款单列表", description = "请款单列表（分页）", method = "GET")
    public Result<Page<OaInvoice>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<OaInvoice> pageResult = oaInvoiceService.listPage(OaInvoice.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取请款单", description = "根据ID获取请款单", method = "GET")
    public Result<OaInvoice> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        OaInvoice invoice = oaInvoiceService.getById(OaInvoice.class, bizNo);
        return Result.OK(invoice);
    }
    
    @RequestMapping("add")
    @Operation(summary = "保存请款单", description = "保存请款单")
    public Result<OaInvoice> add(@RequestBody OaInvoice invoice) {
        return  oaInvoiceService.saveAll(invoice);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<OaInvoice> edit(@RequestBody OaInvoice invoice) {
        OaInvoice dbData = oaInvoiceService.getById(OaInvoice.class,invoice.getBizNo());
        if (!dbData.getOrderStatus().equals(OaEnums.OrderStatus.NEW.getCode())){
            return Result.error("在新增状态下才可修改");
        }
        if (!StringUtils.equals(dbData.getApplyUserDeptId(),invoice.getApplyUserDeptId())){
            Dept levelOneDept = deptService.getLevelOneDept(invoice.getApplyUserDeptId());
            if (levelOneDept != null) {
                invoice.setOrgDesc(StringUtils.isNotBlank(levelOneDept.getDeptDesc())?levelOneDept.getDeptDesc():levelOneDept.getDeptName());
            }
        }
        oaInvoiceService.update(invoice);
        return Result.OK(invoice);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<OaInvoice> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            oaInvoiceService.delete(OaInvoice.class, key);
        }
        return Result.OK();
    }

    /**
     * 编辑审批状态
     * @param invoice
     * @return
     */
    @PutMapping("edit/bpmn/")
    @Operation(summary = "编辑审批状态", description = "编辑审批状态", method = "PUT")
    public Result<OaInvoice> editBpmn(@RequestBody OaInvoice invoice) {
        oaInvoiceService.update(invoice);
        return Result.OK(invoice);
    }

    @RequestMapping("submit/{key}")
    @Operation(summary = "提交申请-工作流",description = "提交申请-工作流")
    public Result<?> change(@PathVariable(name = "key") String key,
                            @RequestParam(value="businessKey", required = false) String businessKey,
                            @RequestBody Map<String, Object> properties) {
        return oaInvoiceService.submit(key, SecurityUtils.getLoginUsername(), businessKey, properties);
    }

    @RequestMapping("submit/Ok")
    @Operation(summary = "提交申请-工作流审核",description = "提交申请-工作流审核")
    public Result<?> submitOK(@RequestBody OaInvoice invoice) {
        return oaInvoiceService.submitOK(invoice);
    }

}
